package controllers

import (
	"blog/models"

	"github.com/astaxie/beego"
)

type ToPicController struct {
	beego.Controller
}

func (this *ToPicController) Get() {
	if checkAccount(this.Ctx) {
		this.Data["Cookie"] = true
	}
	var err error
	this.Data["fenlei"], err = models.GetFenleis()
	if err != nil {
		beego.Error("Fenlei error!")
	}
	this.TplName = "topic.html"
}

func (this *ToPicController) Post() {
	if checkAccount(this.Ctx) {
		this.Data["Cookie"] = true
	}
	text := this.Input().Get("text")
	html := this.Input().Get("html")
	fenleiNumber := this.Input().Get("fenlei")

	topic_heart := this.Input().Get("topic_heard")

	err := models.AddTopic(fenleiNumber, topic_heart, text, html)
	if err != nil {
		beego.Error("插入文章失败!")
	}
	this.Redirect("/", 302)
	return
}
func (this *ToPicController) Add() {
	if checkAccount(this.Ctx) {
		this.Data["Cookie"] = true
	}
	this.Data["fenlei"], _ = models.GetFenleis()

	this.TplName = "topic_add.html"
}

func (this *ToPicController) Query() {
	if checkAccount(this.Ctx) {
		this.Data["Cookie"] = true
	}
	id := this.GetString("id")
	topic, err := models.GetTopic(id)
	if err != nil {
		beego.Error(err)
		this.Redirect("/", 302)
		return
	}

	this.Data["fenlei"], err = models.GetFenleis()
	if err != nil {
		beego.Error("Fenlei error!")
	}

	this.Data["Topic"] = topic
	this.TplName = "topic_query.html"
}
